<template>
  <div class="count-card">
    <div class="header">
      <span>{{ title }}</span>
      <el-tooltip content="tips" placement="top" effect="light">
        <el-icon><Warning /></el-icon>
      </el-tooltip>
    </div>
    <div class="content">
      <span v-if="amount === 'saleroom'">￥</span>
      <span ref="count1Ref">{{ number1 }}</span>
    </div>
    <div class="footer">
      <span>{{ subtitle }}</span>
      <span v-if="amount === 'saleroom'">￥</span>
      <span ref="count2Ref">{{ number2 }}</span>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { CountUp } from 'countup.js'
interface IProps {
  amount?: string
  title?: string
  tips?: string
  number1?: number
  number2?: number
  subtitle?: string
} //动态传入数据,数据设置为可选，要设置默认值

const props = withDefaults(defineProps<IProps>(), {
  title: '商品销售总量',
  tips: '所有商品总销量',
  number1: 12312312,
  number2: 213123,
  subtitle: '商品销售总量'
})
const count1Ref = ref<HTMLElement>()
const count2Ref = ref<HTMLElement>()

//获取类实例
//注意count1Ref.value可能还没有渲染完成
//对dom进行操作可以在onMounted中进行

onMounted(() => {
  const countup1 = new CountUp(count1Ref.value!, props.number1, {})
  const countup2 = new CountUp(count2Ref.value!, props.number2, {})
  countup2.start()

  countup1.start()
})
</script>

<style lang="less" scoped>
.count-card {
  display: flex;
  flex-direction: column;
  padding: 0.2px;
  height: 130px;
  background: white;
  box-shadow: 0.1px rgba(0, 0, 0, 0.1);
}

.header {
  display: flex;
  height: 38px;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.45);
  justify-content: space-between;
  align-items: flex-end;
}
.content {
  display: flex;
  margin-left: 0px;
  font-size: 26px;

  align-items: center;

  flex: 1;
  color: rgba(0, 0, 0, 0.85);
}
.footer {
  display: flex;
  height: 38px;
  line-height: 38px;
  font-size: 14px;
  letter-spacing: 1px;
  color: rgba(0, 0, 0, 0.85);
  border-top: 1px solid #f6f6f6;
}
</style>